﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.TcCommands.TcReplace.CApReplaceObjectManager
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Input;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.TcCommands.TcReplace;

public class CApReplaceObjectManager
{
  private TxObjectList m_previewObjects;

  public CApReplaceObjectManager() => this.m_previewObjects = new TxObjectList();

  public void LoadObject(string externalId)
  {
    TxObjectList txObjectList = new TxObjectList();
    List<string> stringList = new List<string>();
    ITxObject target = this.getTarget();
    if (target == null)
      return;
    ((Collection<ITxObject>) txObjectList).Add(target);
    stringList.Add(externalId);
    this.LoadPreview(new TxTcResourceBrowserObjectDataEx(txObjectList, stringList, 1));
  }

  public void UnLoadObjects()
  {
    if (((Collection<ITxObject>) this.m_previewObjects).Count > 0)
      this.LoadedObject.Delete();
    this.Clear();
  }

  public bool IsObjectLoaded => ((Collection<ITxObject>) this.m_previewObjects).Count > 0;

  public ITxObject LoadedObject
  {
    get
    {
      if (((Collection<ITxObject>) this.m_previewObjects).Count <= 0)
        return (ITxObject) null;
      ITxObject loadedObject = (ITxObject) null;
      foreach (ITxObject previewObject in (Collection<ITxObject>) this.m_previewObjects)
      {
        if (!((Collection<ITxObject>) this.m_previewObjects).Contains((ITxObject) previewObject.Collection))
          loadedObject = previewObject;
      }
      if (loadedObject == null)
        loadedObject = ((Collection<ITxObject>) this.m_previewObjects)[0];
      return loadedObject;
    }
  }

  private void LoadPreview(
    TxTcResourceBrowserObjectDataEx requestForDataImport)
  {
    this.Clear();
    TxApplicationEx.FreezeDisplay();
    TxApplicationEx.FireDocumentUpdateStarted();
    try
    {
      TxObjectList txObjectList = this.InsertObjects(requestForDataImport);
      if (txObjectList == null || ((Collection<ITxObject>) txObjectList).Count <= 0)
        return;
      foreach (ITxDisplayableObject displayableObject in (Collection<ITxObject>) txObjectList)
      {
        ((Collection<ITxObject>) this.m_previewObjects).Add((ITxObject) displayableObject);
        TxDisplayableObjectEx.SetSelectable(displayableObject, false);
        displayableObject.Blank();
      }
      ((TxViewerControl) TxApplication.ViewersManager.GraphicViewer).Refresh();
    }
    finally
    {
      TxApplicationEx.UnfreezeDisplay();
      TxApplicationEx.FireDocumentUpdateEnded();
    }
  }

  private TxObjectList InsertObjects(
    TxTcResourceBrowserObjectDataEx requestForDataImport)
  {
    TxObjectList txObjectList = (TxObjectList) null;
    Cursor overrideCursor = Mouse.OverrideCursor;
    Mouse.OverrideCursor = Cursors.Wait;
    try
    {
      txObjectList = TxTcResourceBrowserImporterEx.InsertResource(requestForDataImport, false);
    }
    catch (Exception ex)
    {
      TxApplication.LogWriter.WriteExceptionLine(ex);
    }
    finally
    {
      Mouse.OverrideCursor = overrideCursor;
    }
    return txObjectList;
  }

  public void Clear()
  {
    TxTcDataImporterEx.ClearPreviewData();
    ((Collection<ITxObject>) this.m_previewObjects).Clear();
  }

  private ITxObject getTarget()
  {
    TxObjectList allDescendants = TxApplication.ActiveDocument.PhysicalRoot.GetAllDescendants((ITxTypeFilter) new TxTypeFilter(typeof (TxCompoundResource)));
    TxObjectList processTargets = this.GetProcessTargets();
    ITxObject target = ((Collection<ITxObject>) allDescendants).Count > 0 ? ((Collection<ITxObject>) allDescendants)[0] : (((Collection<ITxObject>) processTargets).Count > 0 ? ((Collection<ITxObject>) processTargets)[0] : (ITxObject) null);
    switch (target)
    {
      case ITxProcessModelObject _:
      case ITxTcOperation _:
      case TxTcProcess _:
      case TxCompoundResource _:
        return target;
      default:
        return (ITxObject) null;
    }
  }

  private TxObjectList GetProcessTargets()
  {
    TxObjectList directDescendants = TxApplication.ActiveDocument.OperationRoot.GetDirectDescendants((ITxTypeFilter) new TxNoTypeFilter());
    if (TxEngineeringDataInternal.GlobalGetPartsMode() == 2 && ((Collection<ITxObject>) directDescendants).Count > 0 && ((Collection<ITxObject>) directDescendants)[0] is ITxObjectCollection objectCollection)
      directDescendants = objectCollection.GetDirectDescendants((ITxTypeFilter) new TxNoTypeFilter());
    return directDescendants;
  }
}
